/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Lam
 */
public class FileOperator {

    public FileOperator() {
    }

    /**
     * 主要获取文件
     *
     * @return
     */
    public File getFiles() {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Select VCards");
        chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.home")));
        chooser.setMultiSelectionEnabled(true);
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".vcf") || f.isDirectory();
            }

            public @Override
            String getDescription() {

                return "VCard Files";
            }
        });

        int result = chooser.showOpenDialog(null);
        if (result == JFileChooser.CANCEL_OPTION) {
            return null;
        }

        try {
            File files = chooser.getSelectedFile(); // get the file
            return files;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Warning! Could not load the file(s)!", "Warning!", JOptionPane.WARNING_MESSAGE);
            return null;
        }
    }

    /**
     * 主要是调用BufferedReader读取文件
     */
    public String readFile(File file) throws FileNotFoundException, IOException {
//        InputStream in = new FileInputStream(file);
//        String charcodeString = null;
//        byte[] b = new byte[3];
//        in.read(b);
//        if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
//            charcodeString = "utf-8";
//        } else {
//            charcodeString = "gbk";
//        }
//        System.out.println(charcodeString);
//        in.close();
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String temp = null;
        StringBuffer sb = new StringBuffer();
        temp = br.readLine();
        while (temp != null) {
            sb.append(temp + "\n");
            temp = br.readLine();
        }
        br.close();
        return sb.toString();
    }

    /**
     * 保存文件
     */
    public File saveFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File("."));
        fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".vcf") || f.isDirectory();
            }

            public @Override
            String getDescription() {
                return "VCard Files";
            }
        });
        File file = null;
        if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            file = fileChooser.getSelectedFile();
        }
        return file;
    }

}
